package com.example.util;

import com.example.bean.User;
import lombok.extern.slf4j.Slf4j;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

@Slf4j
public class CglibUtil {
    public void test() {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(User.class);
        enhancer.setCallback(new MethodInterceptor() {
            @Override
            public Object intercept(Object obj, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
                log.info("--------------before------------------:" + method.getName());
                Object result = methodProxy.invokeSuper(obj, args );
                log.info("--------------after------------------:" + method.getName());
                return result;
            }
        });
        User hello = (User)enhancer.create();
        hello.toString();
    }
}
